#include "wxGenerateDialog.h"

BEGIN_EVENT_TABLE(wxGenerateDialog, wxDialog)
    EVT_UPDATE_UI(wxID_OK, wxGenerateDialog::onUpdateOkButton)
END_EVENT_TABLE()

wxGenerateDialog::wxGenerateDialog(wxWindow *parent) :
    wxDialog(parent, wxID_ANY, _("Enter generation conditions"), wxDefaultPosition, wxSize(250, 230))
{
    wxBoxSizer *boxSizer = new wxBoxSizer(wxVERTICAL);

    // create x field
    wxBoxSizer* xSizer = new wxBoxSizer(wxHORIZONTAL);
    minXBox = new wxTextCtrl(this, wxID_ANY);
    populate(xSizer, minXBox, _("Min X"), 50);
    xSizer->AddSpacer(10);
    maxXBox = new wxTextCtrl(this, wxID_ANY);
    populate(xSizer, maxXBox, _("Max X"), 50);

    // create y field
    wxBoxSizer* ySizer = new wxBoxSizer(wxHORIZONTAL);
    minYBox = new wxTextCtrl(this, wxID_ANY);
    populate(ySizer, minYBox, _("Min Y"), 50);
    ySizer->AddSpacer(10);
    maxYBox = new wxTextCtrl(this, wxID_ANY);
    populate(ySizer, maxYBox, _("Max Y"), 50);

    // create z field
    wxBoxSizer* zSizer = new wxBoxSizer(wxHORIZONTAL);
    minZBox = new wxTextCtrl(this, wxID_ANY);
    populate(zSizer, minZBox, _("Min Z"), 50);
    zSizer->AddSpacer(10);
    maxZBox = new wxTextCtrl(this, wxID_ANY);
    populate(zSizer, maxZBox, _("Max Z"), 50);

    //create amount field
    wxBoxSizer *amountSizer = new wxBoxSizer(wxHORIZONTAL);
    amountBox = new wxTextCtrl(this, wxID_ANY);
    populate(amountSizer, amountBox, _("Amount of points"), 80);

    //create buttons
    wxSizer* buttonSizer = CreateButtonSizer(wxOK | wxCANCEL);

    // put it all together
    boxSizer->Add(xSizer, 0, wxTOP, 10);
    boxSizer->Add(ySizer, 0, wxTOP, 10);
    boxSizer->Add(zSizer, 0, wxTOP, 10);
    boxSizer->Add(amountSizer,0, wxALIGN_CENTER | wxTOP, 10);
    boxSizer->Add(buttonSizer, 0, wxALIGN_CENTER | wxTOP | wxBOTTOM, 10);

    SetSizer(boxSizer);
    SetMinSize(wxSize(350, 100));
    Fit();
}

wxGenerateDialog::~wxGenerateDialog()
{
    delete minXBox;
    delete maxXBox;
    delete minYBox;
    delete maxYBox;
    delete minZBox;
    delete maxZBox;
    delete amountBox;
}

double wxGenerateDialog::getMinX() const
{
    return minX;
}

double wxGenerateDialog::getMaxX() const
{
    return maxX;
}
double wxGenerateDialog::getMinY() const
{
    return minY;
}
double wxGenerateDialog::getMaxY() const
{
    return maxY;
}
double wxGenerateDialog::getMinZ() const
{
    return minZ;
}
double wxGenerateDialog::getMaxZ() const
{
    return maxZ;
}
double wxGenerateDialog::getAmount() const
{
    return amount;
}

void wxGenerateDialog::onUpdateOkButton(wxUpdateUIEvent& event)
{
    event.Enable(false);

    if(allFieldsArePopulated() && allFieldsAreNumbers())
    {
        event.Enable(true);
    }
}

bool wxGenerateDialog::allFieldsArePopulated() const
{
    return !minXBox->GetValue().empty() &&
           !maxXBox->GetValue().empty() &&
           !minYBox->GetValue().empty() &&
           !maxYBox->GetValue().empty() &&
           !minZBox->GetValue().empty() &&
           !maxZBox->GetValue().empty() &&
           !amountBox->GetValue().empty();
}

bool wxGenerateDialog::allFieldsAreNumbers() const
{
    return minXBox->GetValue().IsNumber() &&
           maxXBox->GetValue().IsNumber() &&
           minYBox->GetValue().IsNumber() &&
           maxYBox->GetValue().IsNumber() &&
           minZBox->GetValue().IsNumber() &&
           maxZBox->GetValue().IsNumber() &&
           amountBox->GetValue().IsNumber();
}

bool wxGenerateDialog::TransferDataFromWindow()
{
    minX = std::atof(minXBox->GetValue().mb_str());
    maxX = std::atof(maxXBox->GetValue().mb_str());

    minY = std::atof(minYBox->GetValue().mb_str());
    maxY = std::atof(maxYBox->GetValue().mb_str());

    minZ = std::atof(minZBox->GetValue().mb_str());
    maxZ = std::atof(maxZBox->GetValue().mb_str());

    amount = std::atof(amountBox->GetValue().mb_str());
    return true;
}

void wxGenerateDialog::populate(wxBoxSizer *sizer, wxTextCtrl *box, const wxString labelValue, int xSize)
{
    wxStaticText* label = new wxStaticText(this, wxID_ANY, labelValue);
    label->SetMinSize(wxSize(xSize, label->GetMinSize().y));

    sizer->Add(label);
    sizer->Add(box);
}




